
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Pbdms.Common.DataObject;
using System.Threading;
using System.Net;
namespace Pbdms.Common.SharedVariables
{
    public  class FriendsControl
    {
        static ReaderWriterLock rwFriends = new ReaderWriterLock();
        private ArrayList friends;

        
        public FriendsControl()
        {
            friends = new ArrayList();

        }

        #region setter

        
        #region delete a friend
        public void deleteFriend(Common.DataObject.Buddy buddy)
        {
            do
            {
                try
                {
                    rwFriends.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsWriterLockHeld);
            try
            {
                for (int i = 0; i < friends.Count; i++)
                {
                    Common.DataObject.Buddy friend = (Common.DataObject.Buddy)friends[i];
                    if (friend.Id == buddy.Id)
                    {
                        friends.RemoveAt(i);
                    }
                }
            }
            finally
            {
                rwFriends.ReleaseWriterLock();
            }



        }
        #endregion
        #region add a friend
        
        public void addFriend(Common.DataObject.Buddy buddy)
        {
            do
            {
                try
                {
                    rwFriends.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsWriterLockHeld);
            try
            {
                Buddy newBuddy = new Buddy(buddy.Id, buddy.Name, buddy.KeyID,buddy.IpAdd,buddy.Trusted,buddy.Authenticated);
                friends.Add(newBuddy);
            }
            finally
            {
                rwFriends.ReleaseWriterLock();
            }



        }
        
        #endregion
        #region set a friend authenticated
        public void setVerifiedFriendIP(string keyID, string ipaddress)
        {
            do
            {
                try
                {
                    rwFriends.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsWriterLockHeld);
            try
            {
                for (int i = 0; i < friends.Count; i++)
                {
                    Common.DataObject.Buddy friend = (Common.DataObject.Buddy)friends[i];
                    if (friend.KeyID.Equals(keyID))
                    {
                        
                        if (friend.Trusted == false)
                        {
                            return;
                        }
                       
                        Buddy newBuddy = new Buddy(friend.Id, friend.Name, friend.KeyID, ipaddress, friend.Trusted,true);
                        friends[i] = newBuddy;
                    }

                }
            }
            finally
            {
                rwFriends.ReleaseWriterLock();
            }
        }
        #endregion
        #region set a friend trustworthy
        public void setABuddyTrustworthy(string keyID, Boolean trusted)
        {
            do
            {
                try
                {
                    rwFriends.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsWriterLockHeld);
            try
            {
                for (int i = 0; i < friends.Count; i++)
                {
                    Common.DataObject.Buddy friend = (Common.DataObject.Buddy)friends[i];
                    if (friend.KeyID.Equals(keyID))
                    {
                        Boolean auth = friend.Authenticated;
                        string ip = friend.IpAdd;
                        if (trusted == false)
                        {
                            ip = null;
                            auth = false;
                        }
                        Buddy newBuddy = new Buddy(friend.Id, friend.Name, friend.KeyID, ip, trusted,auth);
                        friends[i] = newBuddy;
                    }

                }
            }
            finally
            {
                rwFriends.ReleaseWriterLock();
            }
        }
        #endregion
        public void setLogOff(string keyID)
        {
            do
            {
                try
                {
                    rwFriends.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsWriterLockHeld);
            try
            {
                for (int i = 0; i < friends.Count; i++)
                {
                    Common.DataObject.Buddy friend = (Common.DataObject.Buddy)friends[i];
                    if (friend.KeyID.Equals(keyID))
                    {

                        if (friend.Trusted == false)
                        {
                            return;
                        }

                        Buddy newBuddy = new Buddy(friend.Id, friend.Name, friend.KeyID, null, friend.Trusted, false);
                        friends[i] = newBuddy;
                    }

                }
            }
            finally
            {
                rwFriends.ReleaseWriterLock();
            }
        }
        #endregion
        #region change Name
        public void changeName(Buddy buddy)
        {
            do
            {
                try
                {
                    rwFriends.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsWriterLockHeld);
            try
            {
                for (int i = 0; i < friends.Count; i++)
                {
                    Common.DataObject.Buddy friend = (Common.DataObject.Buddy)friends[i];
                    if (friend.KeyID.Equals(buddy.KeyID))
                    {

         
                        Buddy newBuddy = new Buddy(friend.Id, buddy.Name, friend.KeyID,friend.IpAdd, friend.Trusted,friend.Authenticated);
                        friends[i] = newBuddy;
                    }

                }
            }
            finally
            {
                rwFriends.ReleaseWriterLock();
            }
        }
        #endregion
        #region getter
        //return a friend who is AUTHENTICATED
        public ArrayList getAuthenticatedFriendList()
        {
            ArrayList temp = new ArrayList();
            do
            {
                try
                {
                    rwFriends.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsReaderLockHeld);
            try
            {
                foreach (Buddy buddy in friends)
                {
                    if (buddy.Authenticated==true)
                    {
                        Buddy newBuddy = new Buddy(buddy.Id, buddy.Name, buddy.KeyID, buddy.IpAdd,buddy.Trusted,buddy.Authenticated);
                        temp.Add(newBuddy);
                    }
                }
            }
            finally
            {
                rwFriends.ReleaseReaderLock();
            }

            return temp;
        }
       
      
        public Buddy getAFriendByKeyID(string keyID)
        {
            Buddy newBuddy=null;
            do
            {
                try
                {
                    rwFriends.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsReaderLockHeld);
            try
            {
                foreach (Buddy buddy in friends)
                {
                    if (buddy.KeyID.Equals(keyID))
                    {
                        newBuddy = new Buddy(buddy.Id, buddy.Name, buddy.KeyID, buddy.IpAdd,buddy.Trusted,buddy.Authenticated);
                        break;
                    }
                }
            }
            finally
            {
                rwFriends.ReleaseReaderLock();
            }

            return newBuddy;
        }
        public Buddy getAFriendByID(string id)
        {
            Buddy newBuddy = null;
            do
            {
                try
                {
                    rwFriends.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsReaderLockHeld);
            try
            {
                foreach (Buddy buddy in friends)
                {
                    if (buddy.Id==int.Parse(id))
                    {
                        newBuddy = new Buddy(buddy.Id, buddy.Name, buddy.KeyID, buddy.IpAdd, buddy.Trusted, buddy.Authenticated);
                        break;
                    }
                }
            }
            finally
            {
                rwFriends.ReleaseReaderLock();
            }

            return newBuddy;
        }

        public Buddy getAFriendByIP(string IP)
        {
            Buddy newBuddy = null;
            do
            {
                try
                {
                    rwFriends.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwFriends.IsReaderLockHeld);
            try
            {
                foreach (Buddy buddy in friends)
                {
                    if (buddy.IpAdd == null)
                    {
                        continue;
                    }
                    if (buddy.IpAdd.Equals(IP))
                    {
                        newBuddy = new Buddy(buddy.Id, buddy.Name, buddy.KeyID, buddy.IpAdd, buddy.Trusted, buddy.Authenticated);
                        break;
                    }
                }
            }
            finally
            {
                rwFriends.ReleaseReaderLock();
            }

            return newBuddy;
        }
        #endregion


    }

}
